package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.extend.SmsTemplateExtend;
import com.zb.mapper.ZjSmsTemplateMapper;
import com.zb.result.PageUtil;
import com.zb.service.ZjSmsReceiveRoleService;
import com.zb.service.ZjSmsTemplateService;
import com.zb.vo.SmsTemplateDetailVo;
import com.zb.vo.SmsTemplateListSearchVo;
import com.zb.vo.SmsTemplateListVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 商家短信模板表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-03-21
 */
@Service
public class ZjSmsTemplateServiceImpl extends ServiceImpl<ZjSmsTemplateMapper, ZjSmsTemplate> implements ZjSmsTemplateService {
    @Resource
    private ZjSmsTemplateMapper smsTemplateMapper;
    @Resource
    private ZjSmsReceiveRoleService smsReceiveRoleService;

    // 同步短信模板
    @Override
    public void syncSmsTemplate(AccounLoginInfo loginInfo) {
        List<ZjAdmSmsTemplate> admSmsTemplateList = smsTemplateMapper.getAllAdmSmsTemplate();
        List<ZjSmsTemplate> templates = smsTemplateMapper.getByCustAccId(loginInfo.getCustAccId());

        List<ZjSmsTemplate> addTemplates = new ArrayList<>();
        List<ZjSmsTemplate> updateTemplates = new ArrayList<>();
        ZjSmsTemplate template = null;
        for (ZjAdmSmsTemplate admSmsTemplate : admSmsTemplateList) {
            template = templates.stream().filter(f -> Objects.equals(f.getAdmSmsTemplateId(), admSmsTemplate.getId())).findFirst().orElse(null);
            if (null == template) {
                template = new ZjSmsTemplate();
                template.setId(IdWorker.getId());
                template.setCustAccId(loginInfo.getCustAccId());
                template.setAdmSmsTemplateId(admSmsTemplate.getId());
                template.setCode(admSmsTemplate.getCode());
                template.setRelease(admSmsTemplate.getDisabled());
                template.setDisabled(false);
                addTemplates.add(template);
                continue;
            }
            template.setCode(admSmsTemplate.getCode());
            template.setRelease(admSmsTemplate.getDisabled());
            updateTemplates.add(template);
        }
        if (CollectionUtil.isNotEmpty(addTemplates)) this.saveBatch(addTemplates);
        if (CollectionUtil.isNotEmpty(updateTemplates)) this.updateBatchById(updateTemplates);
    }

    // 根据短信编号获取总后台短信模板
    @Override
    public ZjAdmSmsTemplate getAdmSmsTemplateBySmsCode(String smsCode) {
        return smsTemplateMapper.getAdmSmsTemplateBySmsCode(smsCode);
    }

    // 获取短信模板列表
    @Override
    public Map<String, Object> getList(Long custAccId, SmsTemplateListSearchVo search) {
        PageUtil.setPageLimit(search);
        long count = smsTemplateMapper.getListCount(custAccId, search);
        if (count <= 0) {
            return PageUtil.pageList(search.getPage(), search.getLimit(), count, new ArrayList<>());
        }
        List<SmsTemplateExtend> templateExtends = smsTemplateMapper.getList(custAccId, search);
        List<ZjSmsReceiveRole> receiveRoles = smsReceiveRoleService.getRoleTypeBySmsTemplateIds(templateExtends.stream().map(SmsTemplateExtend::getId).toList());
        return PageUtil.pageList(search.getPage(), search.getLimit(), count, templateExtends.stream().map(template -> {
            SmsTemplateListVo vo = new SmsTemplateListVo();
            BeanUtil.copyProperties(template, vo);
            vo.setCode(template.getCode().substring(0, 3) + "******" + template.getCode().substring(template.getCode().length() -3));
            vo.setStatus(template.getDisabled());
            vo.setCreateTime(template.getGmtCreate());
            vo.setUpdateTime(template.getGmtUpdate());
            vo.setRoleTypeStr(receiveRoles.stream().filter(f -> Objects.equals(f.getSmsTemplateId(), template.getId())).map(roleType -> {
                return roleType.getRoleType() == 1? "客服" : roleType.getRoleType() == 2? "指定人员" : roleType.getRoleType() == 3? "服务商负责人":
                        roleType.getRoleType() == 4? "工程师" : "客户";
            }).collect(Collectors.joining("、")));
            return vo;
        }).toList());
    }

    // 获取短信模板详情
    @Override
    public SmsTemplateDetailVo getDetail(Long id) {
        SmsTemplateExtend extend = smsTemplateMapper.getById(id);
        Assert.notNull(extend, "短信模板参数[ " + id + " ]的数据不存在");
        SmsTemplateDetailVo vo = new SmsTemplateDetailVo();
        BeanUtil.copyProperties(extend, vo);
        vo.setStatus(extend.getDisabled());
        vo.setCode(extend.getCode().substring(0, 3) + "******" + extend.getCode().substring(extend.getCode().length() -3));
        return vo;
    }

    // 启用或禁用短信模板
    @Override
    public void updateDisableBatchById(List<Long> ids, boolean status) {
        if (CollectionUtil.isEmpty(ids)) {
            return;
        }
        smsTemplateMapper.updateDisableBatchById(ids, status);
    }

    // 根据短信编号查询短信是否启用
    @Override
    public boolean isEnableByCode(Long custAccId, String smsCode) {
        Boolean enable = smsTemplateMapper.isEnableByCode(custAccId, smsCode);
        return null != enable && !enable;
    }

    // 根据短信模板ID获取短信接收角色类型下拉数据
    @Override
    public List<SelectResultVo> getAdmRoleTypeSelect(Long id) {
        List<ZjAdmSmsReceiveRole> receiveRoleList = smsTemplateMapper.getAdmRoleType(id);
        if (CollectionUtil.isEmpty(receiveRoleList)) {
            return new ArrayList<>();
        }
        return receiveRoleList.stream().map(role -> {
            return new SelectResultVo(Long.parseLong(String.valueOf(role.getRoleType())),
                    role.getRoleType() == 1? "客服" : role.getRoleType() == 2? "指定人员" : role.getRoleType() == 3? "服务商负责人":
                            role.getRoleType() == 4? "工程师" : "客户"
                    );
        }).toList();
    }
}
